package com.tarena.lbs.user.service;

import org.springframework.stereotype.Service;
import java.awt.*;
import java.util.Random;

@Service
public class CaptchaService {

    ThreadLocal<String> threadLocal = new ThreadLocal<>();

    // 生成随机验证码（4位字母数字组合）
    public String generateCode() {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            code.append(chars.charAt(random.nextInt(chars.length())));
        }

        threadLocal.set(code.toString());
        return code.toString();
    }


    // 验证验证码是否正确
    public boolean validateCode(String inputCode) {
        return threadLocal.get().equals(inputCode);
    }

}